package wang.unclecat.datacenter;

import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.mp.metadata.binding.dao.inner.rdbstore.IRdbStoreMetaDataHelper;
import timber.log.Timber;
import wang.unclecat.datacenter.db.AlarmOrmDatabase;

import java.io.File;

import static wang.unclecat.datacenter.db.AlarmsDBOperation.*;

public class AlarmRdbStoreMetaDataHelper implements IRdbStoreMetaDataHelper {
    private final DatabaseHelper manager;
    RdbStore rdbStore;
    public AlarmRdbStoreMetaDataHelper() {
        this.manager =  AlarmsDataAbility.manager;
    }

    @Override
    public RdbStore getRdbStore() {
        if (rdbStore == null) {
            rdbStore = manager.getRdbStore(StoreConfig.newDefaultConfig(AlarmOrmDatabase.DATABASE_NAME), 1, new RdbOpenCallback() {
                @Override
                public void onCreate(RdbStore store) {

                    Timber.d("onCreate() called with: rdbStore = [ %s ]", store.getPath());

                    store.executeSql("CREATE TABLE IF NOT EXISTS `alarms` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `hour` INTEGER , `minutes` INTEGER , `daysofweek` INTEGER , `enabled` INTEGER , `message` TEXT )");
                }

                @Override
                public void onUpgrade(RdbStore rdbStore, int i, int i1) {

                }

                @Override
                public void onCorruption(File databaseFile) {
                    super.onCorruption(databaseFile);
                    Timber.d("onCorruption() called with: databaseFile = [ %s ]", databaseFile);
                }
            });
        }
        return rdbStore;
    }

    @Override
    public String getTable() {
        return "alarms";
    }

    @Override
    public String[] getMetaDataIdentity() {
        return new String[]{COL_HOUR, COL_MINUTE, COL_DAYS, COL_ENABLE, COL_MSG};
    }
}
